/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easy.framework.common.utils.Func;
import com.easy.module.system.entity.SysRoleDept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色和部门关联表 Mapper接口
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
public interface SysRoleDeptMapper extends BaseMapper<SysRoleDept> {

    /**
     * 通过角色ID删除角色和部门关联
     *
     * @param roleId 角色ID
     * @return 结果
     */
    default int deleteByRoleId(Long roleId) {
        return delete(Wrappers.<SysRoleDept>lambdaQuery().eq(SysRoleDept::getRoleId, roleId));
    }


    /**
     * 批量删除角色部门关联信息
     *
     * @param roleIds 需要删除的数据ID
     * @return 结果
     */
    default int deleteBatchByRoleIds(Long[] roleIds) {
        return delete(Wrappers.<SysRoleDept>lambdaQuery().in(SysRoleDept::getRoleId, roleIds));
    }

    /**
     * 批量新增角色部门信息
     *
     * @param roleDeptList 角色部门列表
     * @return 结果
     */
    default int addBatch(List<SysRoleDept> roleDeptList){
        roleDeptList.forEach(roleDept -> insert(roleDept));
        return roleDeptList.size();
    }

    /**
     * 通过角色ID获取部门列表
     *
     * @param roleId 角色ID
     * @return 结果
     */
    default List<Long> getDeptIdListByRoleId(Long roleId){
        List<SysRoleDept> roleDeptList= selectList(Wrappers.<SysRoleDept>lambdaQuery().eq(SysRoleDept::getRoleId, roleId));
        if(Func.isNotEmpty(roleDeptList)){
            return roleDeptList.stream().map(SysRoleDept::getDeptId).collect(Collectors.toList());
        }
        return null;
    }
}
